/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.common.vo.BidItemCacheVo;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.BidSessionManager;
import com.apache.cache.util.Validator;

/**
 * description:  统一暂停活动（修改活动状态、逐一修改暂停标的状态、更新缓存）
 * @author Hou Dayu 创建时间：2016-3-24  
 * 
 */
public class UnityStopSessionPluginImpl implements PluginConnector {

	protected Logger log = Logger.getLogger(getClass());
	protected BidSessionManager bidSessionManager;
	protected BidItemManager bidItemManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		String sessionId = Validator.getDefaultStr(param.getInfoId(), String.valueOf(param.getParams("infoId")));
		ResultEntity entity = new ResultEntity();
		if (Validator.isNull(sessionId)) {
			entity.setResult("false");
			entity.setMessage("缺少方法参数，活动暂停失败");
			return entity;
		}

		ParamsVo<BidSession> svo = new ParamsVo<BidSession>();
		svo.setInfoId(sessionId);
		BidSession session = (BidSession) bidSessionManager.getInfoById(svo);
		if (Validator.isEmpty(session)) {
			entity.setResult("false");
			entity.setMessage("没有获取到活动信息，暂停操作失败");
			return entity;
		}
		String status = session.getStatus();
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("session");
		session.setStatus(Constant.getInstance().getSessionConfig("statusSuspend"));
		if (cache.checkCacheObject(cache.session_prefix + sessionId)) {
			cache.updateCacheObject(session);
		}
		List<String> itemIds = (List) cache.getCacheCloneByKey("bidSessionItemIds_" + sessionId);
		if (!Validator.isEmpty(itemIds)) {
			List list = cache.getObjects(itemIds, cache.item_prefix);
			if (!Validator.isEmpty(list)) {
				for (int i = 0; i < list.size(); i++) {
					BidItemCacheVo cacheVo = (BidItemCacheVo) list.get(i);
					UnityItemPluginHelper.getInstance().pauseItem(cacheVo, false);
				}
			}
		}

		svo.setObj(session);
		bidSessionManager.editInfo(svo);
		//更新该活动下所有（待审核、审核中）中标的状态=已发布
		ParamsVo<BidItem> itemVo = new ParamsVo<BidItem>();
		itemVo.setParams("itemStatus", Constant.getInstance().getItemConfig("statusSuspend"));//<!-- 改状态3：暂停 -->
		itemVo.setParams("sessionId", sessionId);
		//设置要满足状态更新的历史状态标的
		itemVo.setParams("itemStatusStr", new String[] { Constant.getInstance().getItemConfig("statusPubslish") });
		itemVo.setMethodKey("BidItemStatus");
		bidItemManager.editInfo(itemVo);
		entity.setMessage("操作成功");
		entity.setEntity("true");
		entity.setResult("true");

		return entity;
	}

	public void setBidSessionManager(BidSessionManager bidSessionManager) {
		this.bidSessionManager = bidSessionManager;
	}

	public void setBidItemManager(BidItemManager bidItemManager) {
		this.bidItemManager = bidItemManager;
	}

}
